import { computed } from "vue";
import { useRoute } from "vue-router";
import useAppTabsStore, { transRouteToMenu } from "./store";

export default function useAppTabs() {
  const tab = transRouteToMenu(useRoute());
  const store = useAppTabsStore();

  const currentIndex = computed(() => {
    const { type } = tab;
    return store.tabsList.findIndex((e) => e[type] == tab[type]);
  });

  // 设置页内标签标题
  const setTabTitle = (title: string) => {
    store.tabsList[currentIndex.value]["title"] = title;
  };

  // 关闭页内标签
  const closeTab = () => store.closeTab(currentIndex.value);

  return {
    index: currentIndex,
    setTabTitle,
    closeTab,
  };
}
